package com.kun.service_statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.service_statistics.Client.MemberClient;
import com.kun.service_statistics.entity.StatisticsDaily;
import com.kun.service_statistics.mapper.StatisticsDailyMapper;
import com.kun.service_statistics.service.StatisticsDailyService;
import com.kun.utils.R;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-11-24
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private MemberClient memberClient;
    @Override
    public void registerCount(String day) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        R registerR= memberClient.countRegister(day);
        int countRegister = (int) registerR.getData().get("countRegister");
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(countRegister);
        statisticsDaily.setDateCalculated(day);
        int i = RandomUtils.nextInt(100, 200);
        statisticsDaily.setVideoViewNum(i);
        statisticsDaily.setLoginNum(i);
        statisticsDaily.setCourseNum(i);
        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> getshowData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);
        List<String> daraList=new ArrayList<>();
        List<Integer> numList= new ArrayList<>();
        for (StatisticsDaily statisticsDaily : list) {
            daraList.add(statisticsDaily.getDateCalculated());
            switch (type){
                case "login_num":
                    numList.add(statisticsDaily
                    .getLoginNum());
                    break;
                case "video_view_num":
                    numList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "register_num":
                    numList.add(statisticsDaily.getRegisterNum());
                    break;
                case "course_num":
                    numList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;

            }
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("daraList",daraList);
        map.put("numList",numList);
        return map;
    }
}
